AC_PREREQ(2.50)

AC_INIT
AC_CONFIG_SRCDIR([readline.c])
. $srcdir/../GUILE-VERSION
PACKAGE=guile-readline
AM_INIT_AUTOMAKE($PACKAGE, $VERSION, no-define)

AC_PROG_CC
AM_PROG_CC_STDC
AM_PROG_LIBTOOL

dnl Should we check for curses, terminfo, and termlib, too?
for termlib in ncurses termcap ; do
   AC_CHECK_LIB(${termlib}, tgoto,
                [LIBS="-l${termlib} $LIBS"; break])
done

AC_CHECK_LIB(readline, main)
if test $ac_cv_lib_readline_main = no; then
  AC_MSG_WARN([libreadline was not found on your system.])
fi

AC_CHECK_FUNCS(siginterrupt rl_clear_signals rl_cleanup_after_signal rl_pre_input_hook)


AC_MSG_CHECKING(if readline clears SA_RESTART flag for SIGWINCH)
AC_CACHE_VAL(guile_cv_sigwinch_sa_restart_cleared,
AC_TRY_RUN([#include <signal.h>
#include <stdio.h>
#include <readline/readline.h>

int
hook ()
{
  struct sigaction action;

  sigaction (SIGWINCH, NULL, &action);
  rl_cleanup_after_signal();

  /* exit with 0 if readline disabled SA_RESTART */
  exit (action.sa_flags & SA_RESTART);
}

int
main ()
{
  struct sigaction action;

  sigaction (SIGWINCH, NULL, &action);
  action.sa_flags |= SA_RESTART;
  sigaction (SIGWINCH, &action, NULL);

  rl_pre_input_hook = hook;
  readline ("");
}],
guile_cv_sigwinch_sa_restart_cleared=yes,
guile_cv_sigwinch_sa_restart_cleared=no,
guile_cv_sigwinch_sa_restart_cleared=yes))
AC_MSG_RESULT($guile_cv_sigwinch_sa_restart_cleared)
if test $guile_cv_sigwinch_sa_restart_cleared = yes; then
  AC_DEFINE(GUILE_SIGWINCH_SA_RESTART_CLEARED)
fi

AC_CACHE_CHECK([for rl_getc_function pointer in readline],
		 ac_cv_var_rl_getc_function,
		 [AC_TRY_LINK([
#include <stdio.h>
#include <readline/readline.h>],
			      [printf ("%ld", (long) rl_getc_function)],
			      [ac_cv_var_rl_getc_function=yes],
			      [ac_cv_var_rl_getc_function=no])])
if test "${ac_cv_var_rl_getc_function}" = "yes"; then
  AC_DEFINE(HAVE_RL_GETC_FUNCTION)
fi

if test $ac_cv_lib_readline_main = yes \
        -a $ac_cv_var_rl_getc_function = no; then
  AC_MSG_WARN([*** libreadline is too old on your system.])
  AC_MSG_WARN([*** You need readline version 2.1 or later.])
fi

AC_CHECK_FUNCS(strdup)

. $srcdir/LIBGUILEREADLINE-VERSION
AC_SUBST(LIBGUILEREADLINE_INTERFACE_CURRENT)
AC_SUBST(LIBGUILEREADLINE_INTERFACE_REVISION)
AC_SUBST(LIBGUILEREADLINE_INTERFACE_AGE)
AC_SUBST(LIBGUILEREADLINE_INTERFACE)

AC_CONFIG_FILES(Makefile)
AC_OUTPUT
